
<%@page import="java.lang.String"%>
<%@page import="java.lang.String"%>
<%@page import="java.util.Map"%>
<%@page import="com.us.es.util.Utils"%>
<%@page import="java.util.HashSet"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Arrays"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.List"%>
<%@page import="com.es.bxip.domain.Deal"%>
<%@page import="com.es.bxip.service.DealService"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html lang="es">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Bxip</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <!-- Le styles -->
        <link href="resources/css/style.css" rel="stylesheet">
        <script src="resources/js/jquery.js"></script>
        <script src="resources/js/bootstrap.min.js"></script>
        <script src="resources/js/bootstrap-datepicker.js"></script>
        <script src="resources/js/js.js"></script>
        <!--        <script src="resources/js/bxipjs.js"></script>-->
        <script src="resources/media/js/jquery.dataTables.js"></script>
        <style>
            body {
                padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
            }
        </style>
        <link href="resources/css/bootstrap-responsive.css" rel="stylesheet">
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

    </head>


    <body>
        <jsp:include page="header.jsp"/>


        <%


            String selected_tags[] = request.getParameterValues("selected_tags");
            Set<String> session_tags = (Set<String>) session.getAttribute("selected_tags");

            System.out.println("sesioncita de los webos" + session_tags);



            Set<Deal> dealsToShow = new HashSet<Deal>();
            String cat = request.getParameter("cat");
            Set<String> tgs = new HashSet<String>();
            if (cat != null) {
                Integer catId = new Integer(cat);
                dealsToShow = DealService.getDealByCategory(catId);

            } else {

                if (selected_tags != null && selected_tags.length > 0) {
                    tgs.addAll(Arrays.asList(selected_tags));
                    if (session_tags == null || session_tags.isEmpty()) {
                        System.out.println("Primera vez que busca" + session_tags);
                        //Primera vez que busca
                        //Tags seleccionados en la busqueda avanzada. Los añadimos a la session
                        session.setAttribute("selected_tags", tgs);
                        dealsToShow = DealService.getDealsByTags(tgs);
                    }
                    if (session_tags != null && !session_tags.isEmpty() && selected_tags.length > 0) {
                        System.out.println("Los que hay" + session_tags + "Seleccionados: " + tgs);
                        //Ya habia algo en la session de busqueda
                        //comprobamos que los tags no estén en la sesión 
                        System.out.println("Interseccion" + session_tags);
                        session.setAttribute("selected_tags", tgs);
                        dealsToShow = DealService.getDealsByTags(tgs);
                    }

                } else {
                    dealsToShow = DealService.getMostPopular();
                }
            }
        %>

        <div class="container">
            <!--contenido de la izquierda-->

            <div id="left_content" class="span7 offset1">

                <h4>Últimos eventos</h4>
                <%for (Deal d : dealsToShow) {%>
                <div id="event<%=d.getId()%>">               
                    <h4><strong><a href="deal.jsp?id=<%=d.getId()%>"><%=d.getTitle()%></a></strong></h4>

                    <div class="row"><div class="span2">
                            <a href="#" class="thumbnail">
                                <img src="https://upload.wikimedia.org/wikipedia/commons/d/df/Gnome_globe_current_event.svg" height="180" width="260" alt="Deal image">
                            </a>
                        </div>
                        <div class="span5"><p><%
                            String des = d.getDescription();
                            if (des.length() > 300) {
                                des = d.getDescription().substring(0, 299);
                                out.println(des);
                            }%></p>
                            <p><a class="btn" href="deal.jsp?id=<%=d.getId()%>">Ir a la oferta</a></p></div>
                    </div>
                    <div class="row">
                        <div class="span7"><p></p>
                            <p><i class="icon-user"></i> por <a href="#"><%=d.getAuthor()%></a> | <i class="icon-calendar"></i> <%=d.getFechaInicio()%> | <i class="icon-comment"></i> <a href="#">0 Comentarios</a> | <i class="icon-share"></i> 
                                <a href="#">Compartir</a> </br> <i class="icon-tags"></i>Tags : 
                                <%
                                    if (d.getTags() != null)
                                        for (String tag : d.getTags()) {%>
                                <a href="index.jsp?selected_tags=<%=tag%>"><span class="label label-info"><%out.println(tag);%></span></a> <%}%>
                            </p>
                        </div> 
                    </div> 
                </div>
                <hr>
                <%}%>


                <!--                <div id="latest_events">
                
                                </div>
                                <h4>Más populares</h4>
                                <hr>
                                <div id="most_popular">
                
                                </div>
                
                
                                <h4>Finalizan hoy</h4>
                                <hr>
                                <div id="today_events">
                
                                </div>-->


            </div>

            <div id="rigth_content" class="span3 well">
                <a href="addDeal.jsp" class="btn" >Crear evento</a>

                <div id="busqueda">
                    <h5>Búsqueda avanzada</h5>
                    <i class="icon-tags"> </i> Tags:
                    <form method="GET" action="index.jsp"> 
                        <%  Set<String> list = DealService.getAllTags();
                            for (String t : list) {
                                String checked = "";
                                if (tgs.contains(t)) {
                                    checked = "checked";
                                }%>
                        <label class="checkbox">  
                            <input type="checkbox" <%=checked%> name="selected_tags" id="checkBox_<%=t%>" value="<%=t%>"> <span class="label label-info"><%=t%> </span>
                        </label>
                        <%}%>
                        <input type="submit" value="Filter">
                    </form>
                </div>  
            </div>
        </div> 


        <jsp:include page="footer.jsp"/>

        <!-- Le javascript
        ================================================== 
        <!-- Placed at the end of the document so the pages load faster -->

        <!--  <script src="resources/js/bootstrap-transition.js"></script>
           <script src="resources/js/bootstrap-alert.js"></script>
           <script src="resources/js/bootstrap-modal.js"></script>
           <script src="resources/js/bootstrap-dropdown.js"></script>
           <script src="resources/js/bootstrap-scrollspy.js"></script>
           <script src="resources/js/bootstrap-tab.js"></script>
           <script src="resources/js/bootstrap-tooltip.js"></script>
           <script src="resources/js/bootstrap-popover.js"></script>
           <script src="resources/js/bootstrap-button.js"></script>
           <script src="resources/js/bootstrap-collapse.js"></script>
           <script src="resources/js/bootstrap-carousel.js"></script>
           <script src="resources/js/bootstrap-typeahead.js"></script>-->

        <script type="text/javascript">
            //            console.log(localStorage.username);
            if (document.body.className === undefined) {
                document.getElementById('index_login_button').style.display = 'none';
            } else {
                document.getElementById('index_login_button').style.display = '';
            }
        </script>



    </body>
</html>



